package com.yiren.dbaa.module.exam.presenter;

import android.app.Activity;

import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.common.contract.RiskControlContract;
import com.yiren.dbaa.module.common.model.RiskControlModel;
import com.yiren.dbaa.module.exam.contract.ExamStudentContract;
import com.yiren.dbaa.module.exam.api.IExamStudentApi;
import com.yiren.dbaa.module.exam.model.ExamStudentModel;
import com.yiren.dbaa.module.exam.api.impl.ExamStudentApiImpl;
import com.yiren.dbaa.net.bean.StudentBean;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/15
 */
public class ExamStudentPresenter implements ExamStudentContract.Presenter {

    private final Activity activity;
    private final IExamStudentApi iExamStudentApi;
    private final RiskControlContract.Model riskControlModel;
    private final ExamStudentContract.Model examStudentModel;

    public ExamStudentPresenter(Activity activity) {
        this.activity = activity;
        iExamStudentApi = new ExamStudentApiImpl(activity);
        riskControlModel = new RiskControlModel(activity);
        examStudentModel = new ExamStudentModel(activity);
    }

    @Override
    public void getStuQrCode(String model, StudentBean student, ExamStudentContract.View v) {
        LoadDialog.show(activity, "查询中...");
        iExamStudentApi.getStuQrCodeUrl(model, new CommonCallBack() {
            @Override
            public void success(Object result1) {
                v.setStudentQrCode((String) result1);
                iExamStudentApi.checkStuSweepCode(new CommonCallBack() {
                    @Override
                    public void success(Object result2) {
                        iExamStudentApi.removeCallback();
                        v.sweepCodeSuccess((StudentBean) result2);
                    }
                });
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }

            @Override
            public void complete() {
                iExamStudentApi.requestStudentList(new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        List<StudentBean> studentBeans = (List<StudentBean>) result;
                        examStudentModel.sortStudentList(studentBeans, student, new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                LoadDialog.dismiss(activity);
                                v.setStudentList((List<StudentBean>) result);
                            }
                        });
                    }

                    @Override
                    public void fail(String error) {
                        LoadDialog.dismiss(activity);
                        MessageBox.show(activity, error);
                    }
                });
            }
        });
    }

    @Override
    public void getDeviceRiskResult() {
        riskControlModel.getCoachRiskResult(null);
    }

    @Override
    public void filterStuExam(StudentBean student, ExamStudentContract.View v) {
        riskControlModel.forbidStudentExam(student, new RiskControlContract.RiskControlCallBack() {
            @Override
            public void forbidStudent(int type, String reason, String url) {
                if (type == 0) {  //风控不过滤，接下来检测剩余学时
                    if (student.getStudent().contains(".0")) {
                        student.setStudent(student.getStudent().replace(".0", ""));
                    }
                    if (student.getCode() == 0 && student.getIsAllowUse() == 1) {
                        v.showGmks("系统提示", student.getTurnOffTips(), "关闭");
                        return;
                    }
                    //只针对于智能评判课时
                    if (student.getCode() == 3 && Float.parseFloat(student.getRemaining()) < 15) {
                        v.showTimeLess(student, "系统提示", "请注意您的学时已不足，为了不影响您的练车体验，请续费课程后再练车", "继续练车", "购买课时");
                    } else {
                        v.selectStuSuccess(student);
                    }
                } else {
                    v.showRiskControlDialog(type, reason, url);
                }
            }
        });
    }

    @Override
    public void removeCallBack() {
        iExamStudentApi.removeCallback();
    }

}
